﻿using System;
using System.Drawing;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;

namespace GUI
{
	/// <summary>
	/// Interaction logic for Main.xaml
	/// </summary>
	public partial class Main : Window
	{
		private System.Windows.Forms.NotifyIcon _notifyIcon;
		private System.Windows.WindowState _storedWindowState = System.Windows.WindowState.Normal;
		ObjectShelve.Clipboard.IClipboardHook _clipboardHook;

		public Main()
		{
			InitializeComponent();
			Loaded += new RoutedEventHandler(Window1_Loaded);

			_notifyIcon = new NotifyIcon();
			_notifyIcon.BalloonTipText = "The application has been minimized. Click the tray icon to open. ";
			_notifyIcon.BalloonTipTitle = Title;
			_notifyIcon.Text = Title;
			_notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
			_notifyIcon.MouseMove += new MouseEventHandler(_notifyIcon_MouseMove);

			System.IO.Stream stream;
			System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
			stream = asm.GetManifestResourceStream("GUI.Resources.30.png");
			Bitmap img = new Bitmap(stream);
			IntPtr imagePtr = img.GetHicon();
			System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(imagePtr);

			_notifyIcon.Icon = icon;
			_notifyIcon.Click += new EventHandler(_notifyIcon_Click);
		}

		void _notifyIcon_MouseMove(object sender, MouseEventArgs e)
		{

		}

		void Window1_Loaded(object sender, RoutedEventArgs e)
		{
			_clipboardHook = ObjectShelve.Clipboard.ClipboardHook.GetClipboard();
			HwndSource hwnd = PresentationSource.FromVisual(this) as HwndSource;
			_clipboardHook.Hook(hwnd);
			_clipboardHook.ClipboardChanged += new EventHandler<ObjectShelve.Clipboard.ClipboardHookEventArgs>(_clipboardHook_ClipboardChanged);
		}

		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			_clipboardHook.Unhook();
			base.OnClosing(e);
		}

		void _clipboardHook_ClipboardChanged(object sender, ObjectShelve.Clipboard.ClipboardHookEventArgs e)
		{
			string[] formats = e.DataObject.GetFormats(true);
			System.Text.StringBuilder sb = new StringBuilder(100);
			foreach (var s in formats)
			{
				sb.AppendLine(s);
			}
			MessageBox.Show(this, sb.ToString());
		}

		void _notifyIcon_Click(object sender, EventArgs e)
		{
			Show();
			WindowState = _storedWindowState;
		}

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			_notifyIcon.Dispose();
			_notifyIcon = null;
		}

		private void Window_StateChanged(object sender, EventArgs e)
		{
			if (WindowState == WindowState.Minimized)
			{
				Hide();
				if (_notifyIcon != null)
				{
					_notifyIcon.ShowBalloonTip(2000);
				}
				else
				{
					_storedWindowState = WindowState;
				}
			}
		}

		private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			CheckTrayIcon();
		}

		void CheckTrayIcon()
		{
			ShowTrayIcon(!IsVisible);
		}

		void ShowTrayIcon(bool show)
		{
			if (_notifyIcon != null)
			{
				_notifyIcon.Visible = show;
			}
		}
	}
}
